import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shop/tools/httpsClient.dart';
import 'package:shop/tools/screenAdapter.dart';

import '../../../../component/appBar.dart';
import '../controllers/product_list_controller.dart';

class ProductListView extends GetView<ProductListController> {
  const ProductListView({super.key});

  Widget _progressIndicator() {
    if (controller.hasData.value) {
      return Center(child: CircularProgressIndicator());
    } else {
      return Center(child: Text('没有更多数据了'));
    }
  }

  Widget _productListWidget() {
    return Container(
      margin: EdgeInsets.only(top: ScreenAdapter.height(140)),
      child: Obx(
        () => controller.pList.isNotEmpty
            ? ListView.builder(
                controller: controller.scrollController,
                padding: EdgeInsets.fromLTRB(
                  ScreenAdapter.width(26),
                  0,
                  ScreenAdapter.width(26),
                  6,
                ),
                itemCount: controller.pList.length,
                itemBuilder: (content, index) {
                  return Column(
                    children: [
                      Container(
                        // 1. 父容器设置背景色和圆角，添加裁剪属性
                        decoration: BoxDecoration(
                          color: Colors.white, // 统一背景色
                          borderRadius: BorderRadius.circular(10),
                        ),
                        clipBehavior: Clip.antiAlias, // 关键：强制子组件内容不超出圆角
                        // margin: EdgeInsets.only(
                        //   bottom: ScreenAdapter.height(6),
                        // ),
                        child: Row(
                          children: [
                            //   左侧
                            Container(
                              padding: EdgeInsets.all(ScreenAdapter.width(60)),
                              width: ScreenAdapter.width(400),
                              height: ScreenAdapter.height(460),
                              child: Image.network(
                                HttpsClient.photoUrl(
                                  // 'public/upload/0AY3Cpw19ZQ2AkYjbA1tQY-Z.png_200x200.png',
                                  controller.pList[index].pic,
                                ),
                                fit: BoxFit.fitHeight,
                              ),
                            ),
                            //   右侧
                            Expanded(
                              child: Container(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Padding(
                                      padding: EdgeInsets.only(
                                        bottom: ScreenAdapter.height(20),
                                      ),
                                      child: Text(
                                        '${controller.pList[index].title}',
                                        style: TextStyle(
                                          fontSize: ScreenAdapter.fontSize(42),
                                          fontWeight: FontWeight.bold,
                                        ),
                                      ),
                                    ),
                                    Padding(
                                      padding: EdgeInsets.only(
                                        bottom: ScreenAdapter.height(20),
                                      ),
                                      child: Text(
                                        '${controller.pList[index].subTitle}',
                                        style: TextStyle(
                                          fontSize: ScreenAdapter.fontSize(34),
                                        ),
                                      ),
                                    ),
                                    Container(
                                      padding: EdgeInsets.only(
                                        bottom: ScreenAdapter.height(20),
                                      ),
                                      child: Row(
                                        children: [
                                          Expanded(
                                            child: Column(
                                              children: [
                                                Text(
                                                  'CPU',
                                                  style: TextStyle(
                                                    fontSize:
                                                        ScreenAdapter.fontSize(
                                                          34,
                                                        ),
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                                Text(
                                                  "Helio G25",
                                                  style: TextStyle(
                                                    fontSize:
                                                        ScreenAdapter.fontSize(
                                                          34,
                                                        ),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                          Text('|'),
                                          Expanded(
                                            child: Column(
                                              children: [
                                                Text(
                                                  '高清拍摄',
                                                  style: TextStyle(
                                                    fontSize:
                                                        ScreenAdapter.fontSize(
                                                          34,
                                                        ),
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                                Text(
                                                  "1300万像素",
                                                  style: TextStyle(
                                                    fontSize:
                                                        ScreenAdapter.fontSize(
                                                          34,
                                                        ),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                          Text('|'),
                                          Expanded(
                                            child: Column(
                                              children: [
                                                Text(
                                                  '超大屏',
                                                  style: TextStyle(
                                                    fontSize:
                                                        ScreenAdapter.fontSize(
                                                          34,
                                                        ),
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                                Text(
                                                  "6.1寸",
                                                  style: TextStyle(
                                                    fontSize:
                                                        ScreenAdapter.fontSize(
                                                          34,
                                                        ),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                    Padding(
                                      padding: EdgeInsets.only(
                                        bottom: ScreenAdapter.height(20),
                                      ),
                                      child: Text(
                                        '￥${controller.pList[index].price}起',
                                        style: TextStyle(
                                          fontSize: ScreenAdapter.fontSize(34),
                                          fontWeight: FontWeight.bold,
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      (index == controller.pList.length - 1)
                          ? _progressIndicator()
                          : Text(''),
                    ],
                  );
                },
              )
            : _progressIndicator(),
      ),
    );
  }

  Widget _subHeaderWidget() {
    return Positioned(
      left: 0,
      right: 0,
      top: 0,
      child: Obx(
        () => Container(
          height: ScreenAdapter.height(120),
          width: ScreenAdapter.width(1080),
          decoration: BoxDecoration(
            color: Colors.white,
            border: Border(
              bottom: BorderSide(
                width: ScreenAdapter.height(2),
                color: const Color.fromRGBO(233, 233, 233, 0.9),
              ),
            ),
          ),
          child: Row(
            children: controller.subHeaderList.map((value) {
              return Expanded(
                flex: 1,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    InkWell(
                      onTap: () {
                        controller.subHeaderChange(value['id']);
                      },
                      child: Padding(
                        padding: EdgeInsets.fromLTRB(
                          0,
                          ScreenAdapter.height(16),
                          0,
                          ScreenAdapter.height(16),
                        ),
                        child: Text(
                          '${value['title']}',
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            fontSize: ScreenAdapter.fontSize(34),
                            color: controller.selectHeaderId == value['id']
                                ? Colors.red
                                : Colors.black54,
                          ),
                        ),
                      ),
                    ),
                    _showIcon(value['id']),
                  ],
                ),
              );
            }).toList(),
          ),
        ),
      ),
    );
  }

  // 自定义箭头组件
  Widget _showIcon(id) {
    if (id == 2 || id == 3) {
      if (controller.subHeaderList[id - 1]["sort"] == 1) {
        return const Icon(Icons.arrow_drop_down, color: Colors.black54);
      } else {
        return const Icon(Icons.arrow_drop_up, color: Colors.black54);
      }
    } else {}
    return Text('');
  }

  @override
  Widget build(BuildContext context) {
    print(Get.arguments);
    return Scaffold(
      key: controller.scaffoldGlobal,
      endDrawer: const Drawer(child: DrawerHeader(child: Text('右侧筛选'))),
      backgroundColor: const Color.fromRGBO(246, 246, 246, 1),
      appBar: appBar(
        false,
        keyWord: controller.keyWords != null ? controller.keyWords : '',
      ),
      body: Stack(children: [_productListWidget(), _subHeaderWidget()]),
    );
  }
}
